//Power by zhougf (edivista@vip.qq.com)
//技术支持微信号：edivista
var base64Encode = require("base64.modified.js");

Page({
  data: {
    pen: [],
    serviceId: "",
    connectedDeviceId: "", //已连接设备uuid
    readCharacteristic: "",
    writeCharacteristic: "",
    num: []
  },
  // 打开蓝牙
  openBluetooth: function () {
    // 初始化蓝牙适配器
    wx.openBluetoothAdapter({
      success: function (res) {
        console.log(res.errMsg);
       
        // success
        wx.showToast({
          title: "初始化蓝牙适配器成功",
          duration: 2000
        })
      },
      fail: function(err){
        wx.showToast({
          title: "蓝牙不可用",
          duration: 2000
        })
      }
    })
    // 获取本机蓝牙适配器状态
    wx.getBluetoothAdapterState({
      success: function (res) {
        console.log("适配器状态:", res.errMsg)
      }
    })
    wx.onBluetoothAdapterStateChange(function (res) {
      console.log(`adapterState changed, now is`, res)
    })
  },
  // 关闭蓝牙模块
  closeBluetooth: function () {

    wx.closeBluetoothAdapter({
      success: function (res) {
        // success
        console.log('success' + res);
      }
    })
  },
  // 开始搜寻附近的蓝牙外围设备
  startBluetoothDevicesDiscovery: function () {
    wx.startBluetoothDevicesDiscovery({
      success: function (res) {
        console.log(res.errMsg);
        wx.showToast({
          title: "开始搜索",
          duration: 2000
        })
      },
      complete: function (res) {
        console.log(res);
        if (res.errCode == 10001){
          wx.showToast({
            title: '蓝牙不可用',
            duration: 1000
          })
        }
      },
    })
  },
  // 停止搜寻附近的蓝牙外围设备
  stopBluetoothDevicescovery: function () {
    wx.stopBluetoothDevicescovery({
      success: function (res) {
        console.log(res)
      }
    })
  },
  // 获取所有已发现的蓝牙设备
  getBluetoothDevices: function () {
    var that = this;
    wx.getBluetoothDevices({
      success: function (res) {
        console.log(res);
        that.setData({
          pen: res.devices
        })
      },
      fail: function (res) {
        console.log(res);
      }
    })
  },
  // 监听寻找到新设备的事件
  onBluetoothDeviceFound: function () {
    var that = this;
    wx.onBluetoothDeviceFound(function (res) {
      console.log(res)
      that.setData({
        penpro: res
      })
    })
  },
  // 根据uuid获取处于已连接状态的设备
  getConnectedBluetoothDevices: function () {
    wx.getConnectedBluetoothDevices({
      success: function (res) {

      }
    })
  },
  //连接设备
  connectTO: function (e) {
    var that = this;
    console.log(e);
    wx.createBLEConnection({
      deviceId: e.currentTarget.id,
      success: function (res) {
        console.log("连接成功");
        that.setData({
          connectedDeviceId: e.currentTarget.id,
          msg: "已连接" + e.currentTarget.id,
          msg1: e.currentTarget.name,
        })
      },
      fail: function () {
        console.log("连接失败");
      }

    })
    console.log(that.data.connectedDeviceId);
  },
  // 断开与低功耗蓝牙设备的连接
  closeBLEConnection: function () {
    wx.closeBLEConnection({
      deviceId: that.data.connectedDeviceId,
      success: function (res) {
        console.log(res)
      }
    })

  },
  // 监听低功耗蓝牙连接的错误事件，包括设备丢失，连接异常断开等等
  onBLEConnectionStateChanged: function (res) {
    console.log(`device ${res.deviceId} state has changed,connected:${res.connected}`)
  },
  // 获取蓝牙设备所有service（服务）
  getBLEDeviceServices: function () {
    var that = this;
    wx.getBLEDeviceServices({
      deviceId: that.data.connectedDeviceId,
      success: function (res) {
        console.log(res);
        //|| (res.services[i].uuid.indexOf("FE7D") > 0) || (res.services[i].uuid.indexOf("fe7d") > 0)
        for (var i = 0; i < res.services.length; i++) {
          console.log(res.services[i].uuid)
          //  if ((res.services[i].uuid.indexOf("FFF0") > 0) || (res.services[i].uuid.indexOf("fff0") > 0) ) 
          if ((res.services[i].uuid.indexOf("0000eee0-0000-1000-8000-00805f9b34fb") != -1) || (res.services[i].uuid.indexOf("0000EEE0-0000-1000-8000-00805F9B34FB") != -1)) {
            that.setData({
              serviceId: res.services[i].uuid
            })
            console.log(res.services[i].uuid)
          }
        }
        console.log(that.data.serviceId);
        wx.showToast({
          title: "1" + that.data.serviceId + "获取service（服务）成功：",
          duration: 8000
        })
      }
    })
    // 获取蓝牙设备所有characteristic(特征值)
    setTimeout(function () {
      wx.getBLEDeviceCharacteristics({
        deviceId: that.data.connectedDeviceId,
        serviceId: that.data.serviceId,
        success: function (res) {
          wx.showToast({
            title: "获取特征值成功",
            duration: 2000
          })
          //   || res.characteristics[i].uuid.indexOf('1E4D') > 0 || res.characteristics[i].uuid.indexOf('1e4d') > 0
          for (var i = 0; i < res.characteristics.length; i++) {
            console.log(res.characteristics[i].uuid)
            // if (res.characteristics[i].uuid.indexOf('FFF1') > 0 || res.characteristics[i].uuid.indexOf('fff1') > 0  )
            if (res.characteristics[i].uuid.indexOf('0000eee1-0000-1000-8000-00805f9b34fb') != -1 || res.characteristics[i].uuid.indexOf('0000EEE1-0000-1000-8000-00805F9B34FB') != -1) {
              that.setData({
                readCharacteristic: res.characteristics[i].uuid,
                writeCharacteristic: res.characteristics[i].uuid
              })
            }
            //           || res.characteristics[i].uuid.indexOf('8841') > 0
            // if (res.characteristics[i].uuid.indexOf('FFF2') > 0 || res.characteristics[i].uuid.indexOf('fff2') > 0 ) 
            /*if (res.characteristics[i].uuid.indexOf('49535343-aca3-481c-91ec-d85e28a60318') != -1 || res.characteristics[i].uuid.indexOf('49535343-ACA3-481C-91EC-D85E28A60318') != -1)
              {
             that.setData({
               writeCharacteristic : res.characteristics[i].uuid
             })
              }*/
          }
          console.log(that.data.readCharacteristic);
          console.log(that.data.writeCharacteristic);
          wx.onBLECharacteristicValueChange(function (characteristic) {
            console.log('characteristic value changed:', characteristic.value)
          })
        },
        fail: function (res) {
          console.log(res);
        }
      })
    }, 500)

  },
  // 读取read
  readBLECharacteristicValue: function () {
    // 必须在这里的回调才能获取
    wx.onBLECharacteristicValueChange(function (characteristic) {
      console.log('characteristic value comed:', characteristic)
    })
    wx.readBLECharacteristicValue({
      // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId: that.data.connectedDeviceId,
      // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
      serviceId: that.data.serviceId,
      // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
      characteristicId: that.data.readCharacteristic,
      success: function (res) {
        console.log('readBLECharacteristicValue:', res.characteristic.value)
        wx.showToast({
          title: "read",
          duration: 2000
        })
      },
      complete: function (res) {
        console.log(res);
      }
    })
  },
  // 开启notify通道
  notifyBLECharacteristicValueChange: function () {
    var that = this;
    wx.notifyBLECharacteristicValueChange({
      state: true, // 启用 notify 功能
      // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId: that.data.connectedDeviceId,
      // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
      serviceId: that.data.serviceId,
      // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
      characteristicId: that.data.readCharacteristic,
      success: function (res) {
        console.log('notifyBLECharacteristicValueChange success', res.errMsg)
        wx.showToast({
          title: "notify",
          duration: 2000
        })
      },
      complete: function (res) {

      }
    })
    wx.onBLEConnectionStateChange(function (res) {
      // 该方法回调中可以用于处理连接意外断开等异常情况
      console.log(`device ${res.deviceId} state has changed, connected: ${res.connected}`)
    })
    // 监听
    wx.onBLECharacteristicValueChange(function (res) {
      console.log(res);
      console.log(`characteristic ${res.characteristicId} has changed, now is ${res.value}`)
      let hex = Array.prototype.map.call(new Uint8Array(res.value), x => ('00' + x.toString(16)).slice(-2)).join('');
      console.log(hex);
    })
  },

  characteristicSendData: function () {
    var that = this;
    var curIndex = that.data.writeDataIndex;
    console.log('characteristicSendData curIndex:' + curIndex);
    if (curIndex < that.data.writeTime) {
      var writeData = that.data.writeData;
      console.log(writeData[curIndex]);
     // var base64str = base64Encode.encode(writeData[curIndex]);
      const arrayBuffer = wx.base64ToArrayBuffer(writeData[curIndex]);
      wx.writeBLECharacteristicValue({
        // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
        deviceId: that.data.connectedDeviceId,
        // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
        serviceId: that.data.serviceId,
        // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
        characteristicId: that.data.writeCharacteristic,
        // 这里的value是ArrayBuffer类型
        value: arrayBuffer,
        success: function (res) {
          console.log(res);
          curIndex++;
          that.setData({
            writeDataIndex: curIndex
          })
          if (curIndex < that.data.writeTime) {
            that.characteristicSendData();
          }
        },
        complete: function (res) {
          console.log(res);
        }
      });

    }
    return curIndex;
  },

  // 发送命令
  writeBLECharacteristicValue: function () {
    var that = this;

    const buffs = "! 0 200 200 1408 1\r\n" +
    "PAGE - WIDTH 576\r\n" +
    "BOX 0 0 576 664 2\r\n" +
    "LINE 0 88 576 88 1\r\n" +
    "LINE 0 216 576 216 1\r\n" +
    "LINE 0 296 576 296 1\r\n" +
    "LINE 0 440 528 440 1\r\n" +
    "LINE 0 568 528 568 1\r\n" +
    "LINE 0 664 528 664 1\r\n" +
    "LINE 528 296 528 664 1\r\n" +
    "LINE 48 296 48 568 1\r\n" +
    "CENTER\r\n" +
    "BARCODE 128 2 3 80 0 100 363604310467\r\n" +
    "SETSP 12\r\n" +
    "T 8 0 0 188 363604310467\r\n" +
    "SETSP O\r\n" +
    "SETMAG 2 2 \r\n" +
    "T 8 0 0  236 上海 上海市 长宁区\r\n" +
    "SETMAG 1 1 \r\n" +
    "LEFT\r\n" +
    "SETBOLD 1\r\n" +
    "T 4 0 64 320 申大通 13826614987\r\n" +
    "T 4 0 64 363 上海市宝山区共和新路4719弄共\r\n" +
    "T 4 0 64 395 和小区12号306室\r\n" +
    "SETBOLD 0\r\n" +
    "T 8 0 13.6 334.4 收\r\n" +
    "T 8 0 13.6 380.4 件\r\n" +
    "T 8 0 13.6 470.4 发\r\n" +
    "T 8 0 13.6 516.4 件\r\n" +
    "T 8 0 64 464 快小宝 13826614987\r\n" +
    "T 8 0 64 500.8 上海市长宁区北翟路1178号（鑫达商务楼）\r\n" +
    "T 8 0 64 528.8 1号楼305室\r\n" +
    "T 8 0 541.6 400 派\r\n" +
    "T 8 0 541.6 464 件\r\n" +
    "T 8 0 541.6 528 联\r\n" +
    "T 8 0 16 586.4 签收人/ 签收时间\r\n" +
    "T 55 0 16 615.2 你的签字代表您已验收此包裹，并已确认商品信息\r\n" +
    "T 55 0 16 639.2 无误, 包装完好, 没有划痕, 破损等表面质量问题。\r\n" +
    "T 8 0 450 629.2 月  日\r\n" +
    "BOX 0 696 576 968 2\r\n" +
    "LINE 0 776 576 776 1\r\n" +
    "LINE 0 912 528 912 1\r\n" +
    "LINE 48 776 48 912 1\r\n" +
    "LINE 0 968 576 968 1\r\n" +
    "LINE 528 776 528 968 1\r\n" +
    "BARCODE 128 1 3 36 352 712 363604310467\r\n" +
    "SETSP 10\r\n" +
    "T 55 0 352 752 363604310467\r\n" +
    "SETSP 0\r\n" +
    "T 8 0 13.6 810.4 发\r\n" +
    "T 8 0 13.6 856.4 件\r\n" +
    "T 8 0 64 804 快小宝 13826514987\r\n" +
    "T 8 0 64 840.8 上海市长宁区北翟路1178号（鑫达商务楼）\r\n" +
    "T 8 0 64 868.8 1号楼305室\r\n" +
    "T 8 0 541.6 808 客\r\n" +
    "T 8 0 541.6 862 户\r\n" +
    "T 8 0 541.6 916 联\r\n" +
    "T 8 0 16 928 物品：\r\n" +
    "BOX 0 1000 576 1408 2\r\n" +
    "LINE 0 1080 576 1080 1\r\n" +
    "LINE 0 1216 528 1216 1\r\n" +
    "LINE 0 1352 528 1352 1\r\n" +
    "LINE 0 1408 576 1408 1\r\n" +
    "LINE 48 1080 48 1352 1\r\n" +
    "LINE 528 1080 528 1408 1\r\n" +
    "BARCODE 128 1 3 36 352 1016 363604310467\r\n" +
    "SETSP 10\r\n" +
    "T 55 0 352 1056 363604310467 \r\n" +
    "SETSP 0\r\n" +
    "T 8 0 13.6 1114.4 收\r\n" +
    "T 8 0 13.6 1160.4 件\r\n" +
    "T 8 0 13.6 1250.4 发\r\n" +
    "T 8 0 13.6 1296.4 件\r\n" +
    "T 8 0 64 1108 申大通 13826514987\r\n" +
    "T 8 0 64 1144.8 上海市宝山区共和新路4719弄共和小区12\r\n" +
    "T 8 0 64 1172.8 1号楼305室\r\n" +
    "T 8 0 64 1244 快小宝 13826514987\r\n" +
    "T 8 0 64 1280.8 上海市长宁区北翟路1178号（鑫达商务楼）\r\n" +
    "T 8 0 64 1308.8 1号楼305室\r\n" +
    "T 8 0 13.6 1368 物品：\r\n" +
    "T 8 0 541.6 1164.8 寄\r\n" +
    "T 8 0 541.6 1234.8 件\r\n" +
    "T 8 0 541.6 1304.8 联\r\n" +
    "FORM\r\n"+
    "PRINT\r\n";
    /*var typedArray = new Uint8Array(buffs);
    var buffer1 = typedArray.buffer;
    console.log(buffer1);*/


    const base64 = 'ISAwIDIwMCAyMDAgMTQwOCAxDQpQQUdFLVdJRFRIIDU3Ng0KQk9YIDAgMCA1NzYgNjY0IDINCkxJTkUgMCA4OCA1NzYgODggMQ0KTElORSAwIDIxNiA1NzYgMjE2IDENCkxJTkUgMCAyOTYgNTc2IDI5NiAxDQpMSU5FIDAgNDQwIDUyOCA0NDAgMQ0KTElORSAwIDU2OCA1MjggNTY4IDENCkxJTkUgMCA2NjQgNTI4IDY2NCAxDQpMSU5FIDUyOCAyOTYgNTI4IDY2NCAxDQpMSU5FIDQ4IDI5NiA0OCA1NjggMQ0KQ0VOVEVSDQpCQVJDT0RFIDEyOCAyIDMgODAgMCAxMDAgMzYzNjA0MzEwNDY3DQpTRVRTUCAxMg0KVCA4IDAgMCAxODggMzYzNjA0MzEwNDY3DQpTRVRTUCBPDQpTRVRNQUcgMiAyIA0KVCA4IDAgMCAgMjM2IMnPuqMgyc+6o8rQILOkxP7H+A0KU0VUTUFHIDEgMSANCkxFRlQNClNFVEJPTEQgMQ0KVCA0IDAgNjQgMzIwIMnqtPPNqCAxMzgyNjYxNDk4Nw0KVCA0IDAgNjQgMzYzIMnPuqPK0LGmyb3H+Lmyus3QwsK3NDcxOcWqubINClQgNCAwIDY0IDM5NSC6zdChx/gxMrrFMzA2ytINClNFVEJPTEQgMA0KVCA4IDAgMTMuNiAzMzQuNCDK1Q0KVCA4IDAgMTMuNiAzODAuNCC8/g0KVCA4IDAgMTMuNiA0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42IDg2MiC7pw0KVCA4IDAgNTQxLjYgOTE2IMGqDQpUIDggMCAxNiA5Mjggzu/Gt6O6DQpCT1ggMCAxMDAwIDU3NiAxNDA4IDINCkxJTkUgMCAxMDgwIDU3NiAxMDgwIDENCkxJTkUgMCAxMjE2IDUyOCAxMjE2IDENCkxJTkUgMCAxMzUyIDUyOCAxMzUyIDENCkxJTkUgMCAxNDA4IDU3NiAxNDA4IDENCkxJTkUgNDggMTA4MCA0OCAxMzUyIDENCkxJTkUgNTI4IDEwODAgNTI4IDE0MDggMQ0KQkFSQ09ERSAxMjggMSAzIDM2IDM1MiAxMDE2IDM2MzYwNDMxMDQ2Nw0KU0VUU1AgMTANClQgNTUgMCAzNTIgMTA1NiAzNjM2MDQzMTA0NjcgDQpTRVRTUCAwDQpUIDggMCAxMy42IDExMTQuNCDK1Q0KVCA4IDAgMTMuNiAxMTYwLjQgvP4NClQgOCAwIDEzLjYgMTI1MC40ILeiDQpUIDggMCAxMy42IDEyOTYuNCC8/g0KVCA4IDAgNjQgMTEwOCDJ6rTzzaggMTM4MjY1MTQ5ODcgDQpUIDggMCA2NCAxMTQ0Ljggyc+6o8rQsabJvcf4ubK6zdDCwrc0NzE5xaq5srrN0KHH+DEyDQpUIDggMCA2NCAxMTcyLjggMbrFwqUzMDXK0g0KVCA4IDAgNjQgMTI0NCC/7NChsaYgMTM4MjY1MTQ5ODcNClQgOCAwIDY0IDEyODAuOCDJz7qjytCzpMT+x/ixsbXUwrcxMTc4usWjqPbOtO/JzM7xwqWjqQ0KVCA4IDAgNjQgMTMwOC44IDG6xcKlMzA1ytINClQgOCAwIDEzLjYgMTM2OCDO78a3o7oNClQgOCAwIDU0MS42IDExNjQuOCC8xA0KVCA4IDAgNTQxLjYgMTIzNC44ILz+DQpUIDggMCA1NDEuNiAxMzA0LjggwaoNCkZPUk0NClBSSU5UDQo=';

    var length = buffs.length;
    // var sendTimes = 0;
    // if (length > 0) {
    //   var mod = (length % 580);
    //   if (mod == 0) {
    //     sendTimes = parseInt(length / 580);
    //   } else {
    //     sendTimes = parseInt(length / 580) + 1;
    //   }
    //   console.log("length:" + length + " sendTimes:" + sendTimes);
    // } else {
    //   console.log("length is 0");
    // }
    // var sendArray = new Array();
    // for (var i = 0; i < sendTimes; i++) {
    //   if (i != (sendTimes - 1)) {
    //     sendArray[i] = buffs.substr(i * 580, 580);
    //   } else {
    //     sendArray[i] = buffs.substr(i * 580);
    //   }
    // }

    var sendTimes = 4;
    var sendStrBase64 = [];
    sendStrBase64[0] ="ISAwIDIwMCAyMDAgMTQwOCAxDQpQQUdFLVdJRFRIIDU3Ng0KQk9YIDAgMCA1NzYgNjY0IDINCkxJTkUgMCA4OCA1NzYgODggMQ0KTElORSAwIDIxNiA1NzYgMjE2IDENCkxJTkUgMCAyOTYgNTc2IDI5NiAxDQpMSU5FIDAgNDQwIDUyOCA0NDAgMQ0KTElORSAwIDU2OCA1MjggNTY4IDENCkxJTkUgMCA2NjQgNTI4IDY2NCAxDQpMSU5FIDUyOCAyOTYgNTI4IDY2NCAxDQpMSU5FIDQ4IDI5NiA0OCA1NjggMQ0KQ0VOVEVSDQpCQVJDT0RFIDEyOCAyIDMgODAgMCAxMDAgMzYzNjA0MzEwNDY3DQpTRVRTUCAxMg0KVCA4IDAgMCAxODggMzYzNjA0MzEwNDY3DQpTRVRTUCBPDQpTRVRNQUcgMiAyIA0KVCA4IDAgMCAgMjM2IMnPuqMgyc+6o8rQILOkxP7H+A0KU0VUTUFHIDEgMSANCkxFRlQNClNFVEJPTEQgMQ0KVCA0IDAgNjQgMzIwIMnqtPPNqCAxMzgyNjYxNDk4Nw0KVCA0IDAgNjQgMzYzIMnPuqPK0LGmyb3H+Lmyus3QwsK3NDcxOcWqubINClQgNCAwIDY0IDM5NSC6zdChx/gxMrrFMzA2ytINClNFVEJPTEQgMA0KVCA4IDAgMTMuNiAzMzQuNCDK1Q0KVCA4IDAgMTMuNiAzODAuNCC8/g0KVCA4IDAgMTMuNiA0NzAuNCC3og0KV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LjYgODA4IL/NDQpUIDggMCA1NDEuNiA4NjIgu6cNClQgOCAwIDU0MS42IDkxNiDBqg0KVCA4IDAgMTYgOTI4IM7vxrejug0KQk9YIDAgMTAwMCA1NzYgMTQwOCAyDQpMSU5FIDAgMTA4MCA1NzYgMTA4MCAxDQpMSU5FIDAgMTIxNiA1MjggMTIxNiAxDQpMSU5FIDAgMTM1MiA1MjggMTM1MiAxDQpMSU5FIDAgMTQwOCA1NzYgMTQwOCAxDQpMSU5FIDQ4IDEwODAgNDggMTM1MiAxDQpMSU5FIDUyOCAxMDgwIDUyOCAxNDA4IDENCkJBUkNPREUgMTI4IDEgMyAzNiAzNTIgMTAxNiAzNjM2MDQzMTA0NjcNClNFVFNQIDEwDQpUIDU1IDAgMzUyIDEwNTYgMzYzNjA0MzEwNDY3IA0KU0VUU1AgMA0KVCA4IDAgMTMuNiAxMTE0LjQgytUNClQgOCAwIDEzLjYgMTE2MC40ILz+DQpUIDggMCAxMy42IDEyNTAuNCC3og0KVCA4IDAgMTMuNiAxMjk2LjQgvP4NClQgOCAwIDY0IDExMDggyeq0882oIDEzODI2NTE0OTg3IA0KVCA4IDAgNjQgMTE0NC44IMnPuqPK0LGmyb3H+Lmyus3QwsK3NDcxOcWqubK6zdChx/gxMg0KVCA4IDAgNjQgMTE3Mi44IDG6xcKlMzA1ytINClQgOCAwIDY0IA==";
    sendStrBase64[3] = "MTI0NCC/7NChsaYgMTM4MjY1MTQ5ODcNClQgOCAwIDY0IDEyODAuOCDJz7qjytCzpMT+x/ixsbXUwrcxMTc4usWjqPbOtO/JzM7xwqWjqQ0KVCA4IDAgNjQgMTMwOC44IDG6xcKlMzA1ytINClQgOCAwIDEzLjYgMTM2OCDO78a3o7oNClQgOCAwIDU0MS42IDExNjQuOCC8xA0KVCA4IDAgNTQxLjYgMTIzNC44ILz+DQpUIDggMCA1NDEuNiAxMzA0LjggwaoNCkZPUk0NClBSSU5UDQoNCg==";

    
    

    
    that.setData({
      writeData: sendStrBase64,
      writeTime: sendTimes,
      writeDataIndex: 0
    });
    var curIndex = that.characteristicSendData();

    //const testArray = "HShBAgAAAg==";
    // var length = buffs.length;
    // var sendTimes = 0;
    // if (length > 0) {
    //   var mod = (length % 100);
    //   if (mod == 0) {
    //     sendTimes = parseInt(length / 100);
    //   } else {
    //     sendTimes = parseInt(length / 100) + 1;
    //   }
    //   console.log("length:" + length + " sendTimes:" + sendTimes);
    // } else {
    //   console.log("length is 0");
    // }
    // var sendArray = new Array();

    // for (var i = 0; i < sendTimes; i++) {
    //     if (i != (sendTimes - 1)) {
    //       sendArray[i] = buffs.substr(i * 100, 100);
    //     } else {
    //       sendArray[i] = buffs.substr(i * 100);
    //     }
    //     var base64str = base64Encode.encode(sendArray[i]);
    //     const arrayBuffer = wx.base64ToArrayBuffer(base64str);
    //     wx.writeBLECharacteristicValue({
    //       // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
    //       deviceId: that.data.connectedDeviceId,
    //       // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
    //       serviceId: that.data.serviceId,
    //       // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
    //       characteristicId: that.data.writeCharacteristic,
    //       // 这里的value是ArrayBuffer类型
    //       value: arrayBuffer,
    //       success: function (res) {
    //         // wx.showToast({
    //         //   title: "write",
    //         //   duration: 2000
    //         // })
    //         console.log("num" + i + ":" + res); 
    //       },
    //       complete: function (res) {
    //         console.log(res);         
    //       }
    //     });  
    // }


    // const arrayBuffer = wx.base64ToArrayBuffer(base64);
    // console.log("arrayBuffer:" + arrayBuffer)  
    // let dataView1 = new DataView(arrayBuffer)
    // for(var i=0;i<dataView1.byteLength;i++){
    //   console.log(dataView1.getUint8(i))
    // }

    //console.log("byte length:" + dataView1.byteLength);

    // wx.writeBLECharacteristicValue({
    //   // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
    //   deviceId: that.data.connectedDeviceId,
    //   // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
    //   serviceId: that.data.serviceId,
    //   // 这里的 characteristicId 需要在上面的 getBLEDeviceCharacteristics 接口中获取
    //   characteristicId: that.data.writeCharacteristic,
    //   // 这里的value是ArrayBuffer类型
    //   value: arrayBuffer,
    //   success: function (res) {
    //     wx.showToast({
    //       title: "write",
    //       duration: 2000
    //     })
    //   },
    //   complete: function (res) {
    //     console.log(res);
    //   }
    // })
      
    

  },
  onLoad: function (options) {
    //console.log('load');
    const buffs = "! 0 200 200 1408 1\r\n" +
      "PAGE - WIDTH 576\r\n" +
      "BOX 0 0 576 664 2\r\n" +
      "LINE 0 88 576 88 1\r\n" +
      "LINE 0 216 576 216 1\r\n" +
      "LINE 0 296 576 296 1\r\n" +
      "LINE 0 440 528 440 1\r\n" +
      "LINE 0 568 528 568 1\r\n" +
      "LINE 0 664 528 664 1\r\n" +
      "LINE 528 296 528 664 1\r\n" +
      "LINE 48 296 48 568 1\r\n" +
      "CENTER\r\n" +
      "BARCODE 128 2 3 80 0 100 363604310467\r\n" +
      "SETSP 12\r\n" +
      "T 8 0 0 188 363604310467\r\n" +
      "SETSP O\r\n" +
      "SETMAG 2 2 \r\n" +
      "T 8 0 0  236 上海 上海市 长宁区\r\n" +
      "SETMAG 1 1 \r\n" +
      "LEFT\r\n" +
      "SETBOLD 1\r\n" +
      "T 4 0 64 320 申大通 13826614987\r\n" +
      "T 4 0 64 363 上海市宝山区共和新路4719弄共\r\n" +
      "T 4 0 64 395 和小区12号306室\r\n" +
      "SETBOLD 0\r\n" +
      "T 8 0 13.6 334.4 收\r\n" +
      "T 8 0 13.6 380.4 件\r\n" +
      "T 8 0 13.6 470.4 发\r\n" +
      "T 8 0 13.6 516.4 件\r\n" +
      "T 8 0 64 464 快小宝 13826614987\r\n" +
      "T 8 0 64 500.8 上海市长宁区北翟路1178号（鑫达商务楼）\r\n" +
      "T 8 0 64 528.8 1号楼305室\r\n" +
      "T 8 0 541.6 400 派\r\n" +
      "T 8 0 541.6 464 件\r\n" +
      "T 8 0 541.6 528 联\r\n" +
      "T 8 0 16 586.4 签收人/ 签收时间\r\n" +
      "T 55 0 16 615.2 你的签字代表您已验收此包裹，并已确认商品信息\r\n" +
      "T 55 0 16 639.2 无误, 包装完好, 没有划痕, 破损等表面质量问题。\r\n" +
      "T 8 0 450 629.2 月  日\r\n" +
      "BOX 0 696 576 968 2\r\n" +
      "LINE 0 776 576 776 1\r\n" +
      "LINE 0 912 528 912 1\r\n" +
      "LINE 48 776 48 912 1\r\n" +
      "LINE 0 968 576 968 1\r\n" +
      "LINE 528 776 528 968 1\r\n" +
      "BARCODE 128 1 3 36 352 712 363604310467\r\n" +
      "SETSP 10\r\n" +
      "T 55 0 352 752 363604310467\r\n" +
      "SETSP 0\r\n" +
      "T 8 0 13.6 810.4 发\r\n" +
      "T 8 0 13.6 856.4 件\r\n" +
      "T 8 0 64 804 快小宝 13826514987\r\n" +
      "T 8 0 64 840.8 上海市长宁区北翟路1178号（鑫达商务楼）\r\n" +
      "T 8 0 64 868.8 1号楼305室\r\n" +
      "T 8 0 541.6 808 客\r\n" +
      "T 8 0 541.6 862 户\r\n" +
      "T 8 0 541.6 916 联\r\n" +
      "T 8 0 16 928 物品：\r\n" +
      "BOX 0 1000 576 1408 2\r\n" +
      "LINE 0 1080 576 1080 1\r\n" +
      "LINE 0 1216 528 1216 1\r\n" +
      "LINE 0 1352 528 1352 1\r\n" +
      "LINE 0 1408 576 1408 1\r\n" +
      "LINE 48 1080 48 1352 1\r\n" +
      "LINE 528 1080 528 1408 1\r\n" +
      "BARCODE 128 1 3 36 352 1016 363604310467\r\n" +
      "SETSP 10\r\n" +
      "T 55 0 352 1056 363604310467 \r\n" +
      "SETSP 0\r\n" +
      "T 8 0 13.6 1114.4 收\r\n" +
      "T 8 0 13.6 1160.4 件\r\n" +
      "T 8 0 13.6 1250.4 发\r\n" +
      "T 8 0 13.6 1296.4 件\r\n" +
      "T 8 0 64 1108 申大通 13826514987\r\n" +
      "T 8 0 64 1144.8 上海市宝山区共和新路4719弄共和小区12\r\n" +
      "T 8 0 64 1172.8 1号楼305室\r\n" +
      "T 8 0 64 1244 快小宝 13826514987\r\n" +
      "T 8 0 64 1280.8 上海市长宁区北翟路1178号（鑫达商务楼）\r\n" +
      "T 8 0 64 1308.8 1号楼305室\r\n" +
      "T 8 0 13.6 1368 物品：\r\n" +
      "T 8 0 541.6 1164.8 寄\r\n" +
      "T 8 0 541.6 1234.8 件\r\n" +
      "T 8 0 541.6 1304.8 联\r\n" +
      "FORM\r\n" +
      "PRINT\r\n";
    var length = buffs.length;
    var sendTimes = 0;
    if (length > 0) {
      var mod = (length % 580);
      if (mod == 0) {
        sendTimes = parseInt(length / 580);
      } else {
        sendTimes = parseInt(length / 580) + 1;
      }
      console.log("length:" + length + " sendTimes:" + sendTimes);
    } else {
      console.log("length is 0");
    }
    var sendArray = new Array();
    for (var i = 0; i < sendTimes; i++) {
      if (i != (sendTimes - 1)) {
        sendArray[i] = buffs.substr(i * 580, 580);
      } else {
        sendArray[i] = buffs.substr(i * 580);
      }
      var base64str = base64Encode.encode(sendArray[i]);
      console.log(base64str);
      var decodeStr = base64Encode.decode(base64str);
      console.log(decodeStr);
    }

  },
  onReady: function () {
    //console.log('ready')
  },
  onShow: function () {
    // console.log('show')
  },
  onHide: function () {
    // console.log('hide')
  },
  onUnload: function () {
    // console.log('unload')
  },
  onPullDownRefresh: function () {
    // console.log('pulldownrefresh')
  },
  onReachBottom: function () {
    // console.log('reachbottom'
  }

})		